<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { sevenResumeInfo } from '@/api/charts'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    sevenResumeInfo().then(response => {
      const res = response.result
      console.log(res)
      if (!res.sevenTotalMap) {
        res.sevenTotalMap = [0, 0, 0, 0, 0, 0, 0]
      }
      if (!res.sevenNewMap) {
        res.sevenNewMap = [0, 0, 0, 0, 0, 0, 0]
      }
      res.sevenTotalMap.length = Object.keys(res.sevenTotalMap).length
      res.sevenNewMap.length = Object.keys(res.sevenNewMap).length
      this.initChart(res.sevenList.reverse(), Array.from(res.sevenTotalMap).reverse(), Array.from(res.sevenNewMap).reverse())
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(xaxisData, yaxisData1, yaxisData2) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          }
        },
        legend: {
          data: ['新建简历量', '简历总量']
        },
        grid: {
          left: '1%',
          right: '3%',
          bottom: '0%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: xaxisData
          }
        ],
        yAxis: [
          {
            minInterval: 1,
            type: 'value',
            name: '单位/个'
          }
        ],
        series: [
          {
            name: '新建简历量',
            type: 'line',
            areaStyle: {},
            data: yaxisData2
          }, {
            name: '简历总量',
            type: 'line',
            areaStyle: {},
            data: yaxisData1
          }
        ]
      })
    }
  }
}
</script>
